<?php

namespace App\Logic;


use App\Custom\Fillable;
use App\Exception\ValidateException;
use Hyperf\Contract\ContainerInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\Logger;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Log\LoggerInterface;


abstract class AbstractLogic
{
    use Fillable;

    /**
     * @var $container ContainerInterface
     */
    protected $container;

    /**
     * @var $request RequestInterface
     */
    protected $request;

    /**
     * @var $logger LoggerInterface
     */
    protected $logger;


    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->request = $container->get(RequestInterface::class);
        $this->logger = $container->make(LoggerFactory::class)->get("logic");

        //请求参数验证
        $validator = $container->get(ValidatorFactoryInterface::class)->make(
            $this->request->all(), $this->rules(), $this->messages(), $this->attributes()
        );
        if ($validator->fails()){
            throw new ValidateException($validator->errors()->all());
        }

        //属性填充
        $this->fill($validator->validated());
    }

    /**
     * 访问权限验证
     * @return bool
     */
    public function authorize() :bool
    {
        return true;
    }

    /**
     * 验证规则数组
     * @return array
     */
    public function rules() :array
    {
        return [];
    }


    public function attributes() :array
    {
        return [];
    }

    /**
     * 验证消息提示
     * @return array
     */
    public function messages() :array
    {
        return [];
    }
}